package com.nwpu.coursesupport.service.imple;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.criteria.LessonSearchCriteria;
import com.nwpu.coursesupport.criteria.SCCViewSearchCriteria;
import com.nwpu.coursesupport.entity.Course;
import com.nwpu.coursesupport.entity.Lesson;
import com.nwpu.coursesupport.entity.Student;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.LessonType;
import com.nwpu.coursesupport.mapper.LessonMapper;
import com.nwpu.coursesupport.mapper.StudentMapper;
import com.nwpu.coursesupport.pagination.PageBounds;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.tool.ServiceUtil;
import com.nwpu.coursesupport.view.SCCView;
import com.nwpu.coursesupport.view.mapper.SCCViewMapper;

@Service("sccViewService")
public class SCCViewService extends AbstractService{
    @Autowired
    private SCCViewMapper sCCViewMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private LessonMapper lessonMapper;
    
    protected static Logger logger = LoggerFactory.getLogger(SCCViewService.class);


    public JSONResult list(SCCViewSearchCriteria sc){
        JSONResult rst = new JSONResult();
        rst.fail();
       
        if(sc==null){
        	sc = new SCCViewSearchCriteria(); 
        }
        if(sc.getId()<=0){
        	int id=(int) getSession().getAttribute("id");
        	sc.setId(id);
        }
        Student st=this.studentMapper.findById(sc.getId());
        if(st==null){
        	logger.error("该学生不存在");
        	rst.fail("该学生不存在");
        }
        
        if (sc.getPageNo() <= 0){
			sc.setPageNo(1);
		}
        
        int pageSize = 20;
        int totalCount =this.sCCViewMapper.count(sc);
        int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
		if (sc.getPageNo() > pageCount){
			sc.setPageNo(pageCount);
		}
		PageBounds page = new PageBounds();
        page.setNumber(sc.getPageNo());
        page.setSize(pageSize);
        
        List<SCCView> list = this.sCCViewMapper.find(sc,page);        
        rst.addData("list", list);
        rst.addData("pageNo", sc.getPageNo());
        rst.addData("allpages", pageCount);
        rst.success();
        return rst;
    }

//    简易版通知
    //TODO：Inform模块更新后删除
	public JSONResult listInform(SCCViewSearchCriteria sc){
        JSONResult rst = new JSONResult();
        rst.fail();
       
        if(sc==null){
        	sc = new SCCViewSearchCriteria(); 
        }
        if(sc.getId()<=0){
        	int id=(int) getSession().getAttribute("id");
        	sc.setId(id);
        }
        Student st=this.studentMapper.findById(sc.getId());
        if(st==null){
        	logger.error("该学生不存在");
        	rst.fail("该学生不存在");
        }
        
        if (sc.getPageNo() <= 0){
			sc.setPageNo(1);
		}
        
        int pageSize = 20;
        int totalCount =this.sCCViewMapper.count(sc);
        int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
		if (sc.getPageNo() > pageCount){
			sc.setPageNo(pageCount);
		}
		PageBounds page = new PageBounds();
        page.setNumber(sc.getPageNo());
        page.setSize(pageSize);
        
        List<SCCView> list = this.sCCViewMapper.find(sc,page);
        List<Course> informList = new ArrayList<>();//暂时用course代替inform
        if (list != null && list.size() > 0) {
        	
			Iterator<SCCView> iterator = list.iterator();
			SCCView sccView = null;
			String content = null;
			SimpleDateFormat spFormat = new SimpleDateFormat("yyyy-MM-dd");//String转为Date
			while(iterator.hasNext()){
				Course courseExa = new Course();
				String dateString = null;
				sccView = iterator.next();
				content = sccView.getDescription();
				if(content.contains("<before>")){
					dateString = content.substring(content.indexOf("<before>")+8);
					try {
						if(spFormat.parse(dateString).after(new Date())){
							courseExa.setName(sccView.getCourseName());
							courseExa.setDescription(content.substring(0, content.indexOf("<before>")));
							courseExa.setCreateDate(spFormat.parse(dateString));
						}
						
					} catch (ParseException e) {

						logger.error("简易版Inform转换日期出错");
			        	rst.fail("信息出错");
					}
					if (courseExa != null) {
						informList.add(courseExa);
					}					
				}
			}
		}

        rst.addData("inform", informList);
        rst.success();
        return rst;
    }
    
    public JSONResult detailView(SCCViewSearchCriteria sc){
    	 JSONResult rst = new JSONResult();
         rst.fail();
        
         if(sc==null||sc.getId()<=0){
        	 logger.error("参数有误");
         	return rst.fail("参数有误");
         }
         SCCView view= this.sCCViewMapper.findById(sc.getId());
         
         if(view==null){
        	 logger.error("该班级不存在");
         	return rst.fail("该班级不存在");
         }
         
         if(sc.getLessonType()==null){
        	 sc.setLessonType(LessonType.THEORY);
         }
         if (sc.getPageNo() <= 0){
 			sc.setPageNo(1);
 		 }
         LessonSearchCriteria lsc = new LessonSearchCriteria();
         lsc.setClassId(sc.getId());
         lsc.setType(sc.getLessonType());
         int pageSize = 20;
         int totalCount =this.lessonMapper.count(lsc);
         int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
 		if (sc.getPageNo() > pageCount){
 			sc.setPageNo(pageCount);
 		}
 		PageBounds page = new PageBounds();
         page.setNumber(sc.getPageNo());
         page.setSize(pageSize);
         
         List<Lesson> list = this.lessonMapper.find(lsc, page);
         if (list == null){
        	 logger.error("数据查询失败");
             return rst.fail("数据查询失败");
         }
         ServiceUtil.parseLesson(list);
         rst.addData("classView", view);
         rst.addData("courseId", view.getCourseId());
         rst.addData("list", list);
         rst.addData("pageNo", sc.getPageNo());
         rst.addData("allpages", pageCount);
         rst.success();
         return rst;
    }
    
    public JSONResult detail(IDParam p){
    	JSONResult rst = new JSONResult();
        rst.fail();
        if(p==null||p.getId()<=0){
        	logger.error("参数有误");
        	return rst.fail("参数有误");
        }
        SCCView view= this.sCCViewMapper.findById(p.getId());
        
        if(view==null){
        	logger.error("该班级不存在");
        	return rst.fail("该班级不存在");
        }
        rst.addData("classView", view);
        rst.success();
        return rst;
    }

}
